using System;
using System.IO;
using System.Xml.Serialization;

namespace MH.WsdlWorks.ProjectModel
{
    public static class ProjectModelSerializer
    {
        public static WsdlProject Deserialize(string projectFilePath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(WsdlProject));
            WsdlProject wsdlProject = null;
            using (FileStream stream = File.Open(projectFilePath, FileMode.Open, FileAccess.Read))
            {
                wsdlProject = (WsdlProject)serializer.Deserialize(stream);
            }
            
            // chain up the internal parent refs that aren't handled by the xml serializer
            wsdlProject.SetParent(null);
            // set the path to the project file
            wsdlProject.SetPath(projectFilePath);

            return wsdlProject;
        }

        public static void Serialize(WsdlProject wsdlProject, string projectFilePath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(WsdlProject));
            using (FileStream stream = File.Open(projectFilePath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                serializer.Serialize(stream, wsdlProject);
            }
        }
    }
}
